*******************************************************

/*  The Impact of the US-China Trade War on Vietnam's Labor Market
 Pham Phuong Ngoc and Dainn Wie
 
 This is do file create all of the tables and figures in Online Appendix version.

Inputs:

1. master.dta 

*/

/*Packges to Install

ssc install winsor2
ssc install reghdfe
ssc install ftools
ssc install outreg2
ssc install egenmore

*/

clear all
capture log close
set more off

cd "C:\Replication"


********************************************************************************
*** PART 1: Tables in Online Appendix
********************************************************************************

*** Table A1: US Import Tariff Difference against China and Vietnam

use "Data\master.dta", clear

*keep 2017, 2018 and 2019
drop if year<=2016

* All industries
preserve
drop if year==2017
collapse (mean) wt_tariff, by(year isic3)
gen industry=1
gen target=(wt_tariff!=0)
collapse (mean) average=wt_tariff (sd) std=wt_tariff (min) min=wt_tariff (max) max=wt_tariff (sum) industry (sum) target, by(year)
label variable average "Mean"
label variable std "Standard Deviation"
label variable min "Min"
label variable max "Max"
label variable industry "Number of industries"
label variable target "Number of target industries"
gen sample=1
order year sample industry target average std min max
save "Data\All_sample.dta", replace
restore
* Agriculture
preserve
drop if year==2017
keep if agri==1
collapse (mean) wt_tariff, by(year isic3)
gen industry=1
gen target=(wt_tariff!=0)
collapse (mean) average=wt_tariff (sd) std=wt_tariff (min) min=wt_tariff (max) max=wt_tariff (sum) industry (sum) target, by(year)
label variable average "Mean"
label variable std "Standard Deviation"
label variable min "Min"
label variable max "Max"
label variable industry "Number of industries"
label variable target "Number of target industries"
gen sample=2
order year sample industry target average std min max
save "Data\Agri_sample.dta", replace
restore
* Manufacturing sectors
preserve
drop if year==2017
keep if manu==1
collapse (mean) wt_tariff, by(year isic3)
gen industry=1
gen target=(wt_tariff!=0)
collapse (mean) average=wt_tariff (sd) std=wt_tariff (min) min=wt_tariff (max) max=wt_tariff (sum) industry (sum) target, by(year)
label variable average "Mean"
label variable std "Standard Deviation"
label variable min "Min"
label variable max "Max"
label variable industry "Number of industries"
label variable target "Number of target industries"
gen sample=3
order year sample industry target average std min max
save "Data\Manu_sample.dta", replace
restore
* Trading sectors
preserve
drop if year==2017
keep if trading==1
collapse (mean) wt_tariff, by(year isic3)
gen industry=1
gen target=(wt_tariff!=0)
collapse (mean) average=wt_tariff (sd) std=wt_tariff (min) min=wt_tariff (max) max=wt_tariff (sum) industry (sum) target, by(year)
label variable average "Mean"
label variable std "Standard Deviation"
label variable min "Min"
label variable max "Max"
label variable industry "Number of industries"
label variable target "Number of target industries"
gen sample=4
order year sample industry target average std min max
save "Data\Trade_sample.dta", replace
restore
* Combine to get Table 1
preserve
use "Data\All_sample.dta", clear
append using "Data\Agri_sample.dta"
append using "Data\Manu_sample.dta"
append using "Data\Trade_sample.dta"
label define sample 1"All" 2"Agriculture" 3"Manufacturing" 4"Trading"
label values sample sample
export excel using "Tables\TableA1.xlsx", replace firstrow(varlabels)
erase "Data\All_sample.dta"
erase "Data\Agri_sample.dta"
erase "Data\Manu_sample.dta"
erase "Data\Trade_sample.dta"
restore

********************************************************************************
*** Table A2: Summary Statistics 

* Open LFS dataset
use "Data\master.dta", clear

*keep 2017, 2018 and 2019
drop if year<=2016

* calculate the mean and standard deviation
* All sample
preserve
collapse informal uninsured age exp expsquare sex schoolyear area agri noag main_workinghours second lnincome (count) nobs=year [weight=weight_final]
gen sample=1
gen statistic=1
save "Data\All_sample_mean.dta", replace
restore

preserve
collapse (sd)informal uninsured age exp expsquare sex schoolyear area agri noag main_workinghours second lnincome (count) nobs=year [weight=weight_final]
gen sample=1
gen statistic=2
save "Data\All_sample_sd.dta", replace
restore

* Before trade war
preserve
keep if year==2017
collapse informal uninsured age exp expsquare sex schoolyear area agri noag main_workinghours second lnincome (count) nobs=year [weight=weight_final]
gen sample=2
gen statistic=1
save "Data\before_mean.dta", replace
restore

preserve
keep if year==2017
collapse (sd) informal uninsured age exp expsquare sex schoolyear area agri noag main_workinghours second lnincome (count) nobs=year [weight=weight_final]
gen sample=2
gen statistic=2
save "Data\before_sd.dta", replace
restore

* After trade war
preserve
keep if year ==2018 | year==2019
collapse informal uninsured age exp expsquare sex schoolyear area agri noag main_workinghours second lnincome (count) nobs=year [weight=weight_final]
gen sample=3
gen statistic=1
save "Data\after_mean.dta", replace
restore

preserve
keep if year ==2018 | year==2019
collapse (sd) informal uninsured age exp expsquare sex schoolyear area agri noag main_workinghours second lnincome (count) nobs=year [weight=weight_final]
gen sample=3
gen statistic=2
save "Data\after_sd.dta", replace
restore

* append data together 
preserve
use "Data\All_sample_mean.dta", clear
append using "Data\All_sample_sd.dta"
append using "Data\before_mean.dta"
append using "Data\before_sd.dta"
append using "Data\after_mean.dta"
append using "Data\after_sd.dta"

order sample statistic
sort sample statistic
label define sample_lbl 1"All" 2"Before Trade War" 3"After Trade War"
label values sample sample_lbl
label define stat_lbl 1"Mean" 2 "Std.Dev"
label values statistic stat_lbl

label variable informal "informal worker"
label variable uninsured "Worker without insurance"
label variable age "Age"
label variable exp "Experience"
label variable expsquare "Square of Experience"
label variable area "Indicator for urban"
label variable sex "Indicator for female"
label variable schoolyear "Number of schooling year"
label variable agri "Indicator for agriculture, forestry and aquaculture"
label variable noag "Indicator for Non-Agriculture"
label variable main_workinghours "working hours per week (main job)"
label variable second "Indicator for more than one job"
label variable lnincome "ln(hourly income)"
label variable nobs "Number of observations"

export excel using "Tables\TableA2.xlsx", replace firstrow(varlabels)
erase "Data\All_sample_mean.dta"
erase "Data\All_sample_sd.dta"
erase "Data\before_mean.dta"
erase "Data\before_sd.dta"
erase "Data\after_mean.dta"
erase "Data\after_sd.dta"
restore

********************************************************************************
*** Table A3: Comparison of Job Characteristics across Types of Employment

* Open LFS dataset
use "Data\master.dta", clear

*keep 2017, 2018 and 2019
drop if year<=2016

* Compare informal vs Formal workers
preserve
collapse main_workinghours workinghours schoolyear rincome second [weight=weight_final], by (informal)
gen sample=1
save "Data\TableA3 sample 1.dta", replace
restore

preserve
keep if noag==1
collapse main_workinghours workinghours schoolyear rincome second [weight=weight_final], by (informal)
gen sample=2
save "Data\TableA3 sample 2.dta", replace
restore

preserve
keep if trading==1
collapse main_workinghours workinghours schoolyear rincome second [weight=weight_final], by (informal)
gen sample=3
save "Data\TableA3 sample 3.dta", replace
restore

preserve
* append the dataset
use "Data\TableA3 sample 1.dta", clear
append using "Data\TableA3 sample 2.dta"
append using "Data\TableA3 sample 3.dta"

label define sample_lbl 1"All" 2"Non-Agriculture" 3"Trading"
label values sample sample_lbl

label define informal_lbl 0"Formal" 1"Informal"
label values informal informal_lbl

label variable sample "Set of industries"
label variable informal "Indicator for informal workers"
label variable main_workinghours "Number of working hours per week in main job"
label variable workinghours "Number of working hours per week in all jobs"
label variable schoolyear "Number of schooling year"
label variable rincome "Hourly income in main job with CPI adjusted"
label variable second "Share with more than ome job"

order sample
export excel using "Tables\TableA3A.xlsx", replace firstrow(varlabels)
erase "Data\TableA3 sample 1.dta"
erase "Data\TableA3 sample 2.dta"
erase "Data\TableA3 sample 3.dta"
restore

* Compare insuranced and uninsured workers
preserve
collapse main_workinghours workinghours schoolyear rincome second [weight=weight_final], by (uninsured)
gen sample=1
save "Data\TableB3 sample 1.dta", replace
restore

preserve
keep if noag==1
collapse main_workinghours workinghours schoolyear rincome second [weight=weight_final], by (uninsured)
gen sample=2
save "Data\TableB3 sample 2.dta", replace
restore

preserve
keep if trading==1
collapse main_workinghours workinghours schoolyear rincome second [weight=weight_final], by (uninsured)
gen sample=3
save "Data\TableB3 sample 3.dta", replace
restore

preserve
* append the dataset
use "Data\TableB3 sample 1.dta", clear
append using "Data\TableB3 sample 2.dta"
append using "Data\TableB3 sample 3.dta"

label define sample_lbl 1"All" 2"Non-Agriculture" 3"Trading"
label values sample sample_lbl

label define uninsured_lbl 0"Insured" 1"Uninsured"
label values uninsured uninsured_lbl

label variable sample "Set of industries"
label variable uninsured "Indicator for uninsured workers"
label variable main_workinghours "Number of working hours per week in main job"
label variable workinghours "Number of working hours per week in all jobs"
label variable schoolyear "Number of schooling year"
label variable rincome "Hourly income in main job with CPI adjusted"
label variable second "Share with more than ome job"

order sample
export excel using "Tables\TableA3B.xlsx", replace firstrow(varlabels)
erase "Data\TableB3 sample 1.dta"
erase "Data\TableB3 sample 2.dta"
erase "Data\TableB3 sample 3.dta"
restore

********************************************************************************
*** Table A4: The impact of Trade war on Controlling for Pre-Trends
use "Data\master.dta", clear

preserve
* interact the household sampling weights with the indicator for informal workers
generate hhwt_informal=weight_final * informal

* create a national level 
egen emp_total= sum (weight_final), by (year isic3)
egen infor_total=sum (hhwt_informal), by (year isic3)

* calculate the share of informal workers
gen sinformal = (infor_total/emp_total) 

collapse (mean) sinformal, by (year isic3)

keep if year==2014 | year==2017
reshape wide sinformal, i(isic3) j(year)
gen diff_informal=sinformal2017-sinformal2014
label var diff_informal "Difference of informal share between 2014 and 2017"

keep isic3 diff_informal
save "Data\informal_share.dta", replace
restore

preserve
* interact the household sampling weights with the indicator for uninsured workers
generate hhwt_uninsured=weight_final * uninsured

* create a national level 
egen emp_total= sum (weight_final), by (year isic3)
egen uninsured_total=sum (hhwt_uninsured), by (year isic3)

* calculate the share of uninsured workers
gen suninsured = (uninsured_total/emp_total) 

collapse (mean) suninsured, by (year isic3)

keep if year==2014 | year==2017
reshape wide suninsured, i(isic3) j(year)
gen diff_uninsured=suninsured2017-suninsured2014
label var diff_uninsured "Difference of uninsured share between 2014 and 2017"

keep isic3 diff_uninsured
save "Data\uninsured_share.dta", replace
restore

* drop year 2016 and 2015
drop if year<=2016

sum wt_tariff if wt_tariff!=0
sum wt_tariff if wt_tariff!=0 & isic3<=332
sum wt_tariff if wt_tariff!=0 & noag==1
sum wt_tariff if wt_tariff!=0 & trading==1

label variable wt_tariff "The weighted tariff difference"
label variable tariff_manu "Tariff difference * Manufacturing"
label variable manu "Manufacturing"
label variable exp "Experience"
label variable expsquare "Square of Experience"
label variable sex "Female"
label variable schoolyear "Schooling years"
label variable area "Urban"
label variable informal "Informal"
label variable uninsured "Uninsured"
label variable tariff_informal "Tariff difference * Informal workers"
label variable tariff_uninsured "Tariff difference * Uninsured workers"

merge m:1 isic3 using informal_share.dta, keep (match master) generate (merge1)
merge m:1 isic3 using uninsured_share.dta, keep (match master) generate (merge2)

gen year_2018=(year==2018)
gen year_2019=(year==2019)

gen inter1 = diff_informal*year_2018
gen inter2 = diff_informal*year_2019

label var inter1 "Informal Share change*Year=2018"
label var inter2 "Informal Share change*Year=2019"

gen inter3 = diff_uninsured*year_2018
gen inter4 = diff_uninsured*year_2019

label var inter3 "Uninsured Share change*Year=2018"
label var inter4 "Uninsured Share change*Year=2019"

* Run regression with dependent variable informal
reghdfe informal wt_tariff inter1 inter2 exp expsquare sex schoolyear area [pweight=weight_final], absorb(isic3 year province) cluster (isic3)

outreg2 using "Tables\TableA4A.doc", label dec(3) replace ctitle(All) addtext(Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

reghdfe informal wt_tariff inter1 inter2 exp expsquare sex schoolyear area [pweight=weight_final] if noag==1, absorb(isic3 year province) cluster (isic3)
outreg2 using "Tables\TableA4A.doc", label dec(3) append ctitle(Excluding agriculture and fishing) addtext(Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

reghdfe informal wt_tariff inter1 inter2 exp expsquare sex schoolyear area [pweight=weight_final] if trading==1, absorb(isic3 year province) cluster (isic3)
outreg2 using "Tables\TableA4A.doc", label dec(3) append ctitle(Trading) addtext(Industry FE, Yes, Year FE, Yes, Province FE, Yes) 


* Run regression with dependent variable informal
reghdfe uninsured wt_tariff inter3 inter4 exp expsquare sex schoolyear area [pweight=weight_final], absorb(isic3 year province) cluster (isic3)

outreg2 using "Tables\TableA4B.doc", label dec(3) replace ctitle(All) addtext(Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

reghdfe uninsured wt_tariff inter3 inter4 exp expsquare sex schoolyear area [pweight=weight_final] if noag==1, absorb(isic3 year province) cluster (isic3)
outreg2 using "Tables\TableA4B.doc", label dec(3) append ctitle(Excluding agriculture and fishing) addtext(Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

reghdfe uninsured wt_tariff inter3 inter4 exp expsquare sex schoolyear area [pweight=weight_final] if trading==1, absorb(isic3 year province) cluster (isic3)
outreg2 using "Tables\TableA4B.doc", label dec(3) append ctitle(Trading) addtext(Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

********************************************************************************
*** Table A5: The impact of Trade war on workers engaged in household business
* Open LFS dataset
use "Data\master.dta", clear

* drop year 2016 and 2015
drop if year<=2016

sum wt_tariff if wt_tariff!=0
sum wt_tariff if wt_tariff!=0 & isic3<=332
sum wt_tariff if wt_tariff!=0 & noag==1
sum wt_tariff if wt_tariff!=0 & trading==1

label variable wt_tariff "The weighted tariff difference"
label variable tariff_manu "Tariff difference * Manufacturing"
label variable manu "Manufacturing"
label variable exp "Experience"
label variable expsquare "Square of Experience"
label variable sex "Female"
label variable schoolyear "Schooling years"
label variable area "Urban"
label variable informal "Informal"
label variable uninsured "Uninsured"
label variable tariff_informal "Tariff difference * Informal workers"
label variable tariff_uninsured "Tariff difference * Uninsured workers"

gen household_bus=.
replace household_bus=1 if ownership==1 | ownership==2
replace household_bus=0 if ownership==3 | ownership==4 | ownership==5 | ownership==6

reghdfe household_bus wt_tariff exp expsquare sex schoolyear area [pweight=weight_final], absorb(isic3 year province) cluster (isic3)

outreg2 using "Tables\TableA5.doc", label dec(3) replace ctitle(All) addtext(Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

reghdfe household_bus wt_tariff exp expsquare sex schoolyear area [pweight=weight_final] if noag==1, absorb(isic3 year province) cluster (isic3)
outreg2 using "Tables\TableA5.doc", label dec(3) append ctitle(Excluding agriculture and fishing) addtext(Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

reghdfe household_bus wt_tariff exp expsquare sex schoolyear area [pweight=weight_final] if trading==1, absorb(isic3 year province) cluster (isic3)
outreg2 using "Tables\TableA5.doc", label dec(3) append ctitle(Trading) addtext(Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

********************************************************************************
*** Table A6: Impact of Trade war on the Share of Uninsured workers - Firm level Analysis without Fixed Effects

use "Data\master_ves.dta", clear
drop if year<=2016

generate noag=(isic3>32)

sum wt_tariff if wt_tariff!=0
sum wt_tariff if wt_tariff!=0 & noag==1
sum wt_tariff if wt_tariff!=0 & trading==1

generate cur_uninsured = cur_employee - cur_insurance

generate share = cur_uninsured / cur_employee

label variable wt_tariff "The weighted tariff difference"

reghdfe share wt_tariff, absorb(province isic3 year) cluster (isic3)
outreg2 using "Tables\TableA6.doc", label dec(3) replace ctitle(All) addtext(Firm FE, No, Industry FE, Yes, Year FE, Yes, Province FE, Yes)

reghdfe share wt_tariff if noag==1, absorb(province isic3 year) cluster (isic3)
outreg2 using "Tables\TableA6.doc", label dec(3) append ctitle(Excluding agriculture and fishing) addtext(Firm FE, No, Industry FE, Yes, Year FE, Yes, Province FE, Yes)

reghdfe share wt_tariff if trading==1, absorb(province isic3 year) cluster (isic3)
outreg2 using "Tables\TableA6.doc", label dec(3) append ctitle(Trading) addtext(Firm FE, No, Industry FE, Yes, Year FE, Yes, Province FE, Yes)

keep if cur_employee>100

reghdfe share wt_tariff, absorb(province isic3 year) cluster (isic3)
outreg2 using "Tables\TableA6.doc", label dec(3) append ctitle(All) addtext(Firm FE, No, Industry FE, Yes, Year FE, Yes, Province FE, Yes)

reghdfe share wt_tariff if noag==1, absorb(province isic3 year) cluster (isic3)
outreg2 using "Tables\TableA6.doc", label dec(3) append ctitle(Excluding agriculture and fishing) addtext(Firm FE, No, Industry FE, Yes, Year FE, Yes, Province FE, Yes)

reghdfe share wt_tariff if trading==1, absorb(province isic3 year) cluster (isic3)
outreg2 using "Tables\TableA6.doc", label dec(3) append ctitle(Trading) addtext(Firm FE, No, Industry FE, Yes, Year FE, Yes, Province FE, Yes)

********************************************************************************
*** Table A7: Impact of Trade war on the Share of Uninsured Workers - using Firms appear in all years

use "Data\master_ves.dta", clear
drop if year<=2016

by ID, sort: egen obs=count(ID)
keep if obs==3

generate noag=(isic3>32)

sum wt_tariff if wt_tariff!=0
sum wt_tariff if wt_tariff!=0 & noag==1
sum wt_tariff if wt_tariff!=0 & trading==1

generate cur_uninsured = cur_employee - cur_insurance

generate share = cur_uninsured / cur_employee

label variable wt_tariff "The weighted tariff difference"

reghdfe share wt_tariff, absorb(ID province isic3 year) cluster (isic3)
outreg2 using "Tables\TableA7.doc", label dec(3) replace ctitle(All) addtext(Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)

reghdfe share wt_tariff if noag==1, absorb(ID province isic3 year) cluster (isic3)
outreg2 using "Tables\TableA7.doc", label dec(3) append ctitle(Excluding agriculture and fishing) addtext(Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)

reghdfe share wt_tariff if trading==1, absorb(ID province isic3 year) cluster (isic3)
outreg2 using "Tables\TableA7.doc", label dec(3) append ctitle(Trading) addtext(Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)

keep if cur_employee>100

reghdfe share wt_tariff, absorb(ID province isic3 year) cluster (isic3)
outreg2 using "Tables\TableA7.doc", label dec(3) append ctitle(All) addtext(Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)

reghdfe share wt_tariff if noag==1, absorb(ID province isic3 year) cluster (isic3)
outreg2 using "Tables\TableA7.doc", label dec(3) append ctitle(Excluding agriculture and fishing) addtext(Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)

reghdfe share wt_tariff if trading==1, absorb(ID province isic3 year) cluster (isic3)
outreg2 using "Tables\TableA7.doc", label dec(3) append ctitle(Trading) addtext(Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)

********************************************************************************
*** Table A8: Impact of Trade War on Share of Uninsured Workers – Firm-level Analysis By Number of Employees

use "Data\master_ves.dta", clear
drop if year<=2016

generate noag=(isic3>32)

sum wt_tariff if wt_tariff!=0
sum wt_tariff if wt_tariff!=0 & noag==1
sum wt_tariff if wt_tariff!=0 & trading==1

generate cur_uninsured = cur_employee - cur_insurance

generate share = cur_uninsured / cur_employee

label variable wt_tariff "The weighted tariff difference"

keep if cur_employee>100

gen cohort_employee=.
replace cohort_employee=2 if cur_employee>100 & cur_employee<=300
replace cohort_employee=3 if cur_employee>300 & cur_employee<=500 
replace cohort_employee=4 if cur_employee>500 


forvalues i=2/4 {
	reghdfe share wt_tariff if cohort_employee==`i', absorb(ID province isic3 year) cluster (isic3)
outreg2 using "Tables\TableA8_`i'.doc", label dec(3) replace ctitle(All) addtext(Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)

reghdfe share wt_tariff if noag==1 & cohort_employee==`i', absorb(ID province isic3 year) cluster (isic3)
outreg2 using "Tables\TableA8_`i'.doc", label dec(3) append ctitle(Excluding agriculture and fishing) addtext(Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)

reghdfe share wt_tariff if trading==1 & cohort_employee==`i', absorb(ID province isic3 year) cluster (isic3)
outreg2 using "Tables\TableA8_`i'.doc", label dec(3) append ctitle(Trading) addtext(Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)
}

********************************************************************************
*** Table A9: Impact of Trade War on the Number of Employees – Firm-level Analysis

use "Data\master_ves.dta", clear
drop if year<=2016

generate noag=(isic3>32)

sum wt_tariff if wt_tariff!=0
sum wt_tariff if wt_tariff!=0 & noag==1
sum wt_tariff if wt_tariff!=0 & trading==1


label variable wt_tariff "The weighted tariff difference"

reghdfe loglabor wt_tariff, absorb(ID province isic3 year) cluster (isic3)
outreg2 using "Tables\TableA9.doc", label dec(3) replace ctitle(All) addtext(Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)

reghdfe loglabor wt_tariff if noag==1, absorb(ID province isic3 year) cluster (isic3)
outreg2 using "Tables\TableA9.doc", label dec(3) append ctitle(Excluding agriculture and fishing) addtext(Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)

reghdfe loglabor wt_tariff if trading==1, absorb(ID province isic3 year) cluster (isic3)
outreg2 using "Tables\TableA9.doc", label dec(3) append ctitle(Trading) addtext(Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)

keep if cur_employee>100

reghdfe loglabor wt_tariff, absorb(ID province isic3 year) cluster (isic3)
outreg2 using "Tables\TableA9.doc", label dec(3) append ctitle(All) addtext(Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)

reghdfe loglabor wt_tariff if noag==1, absorb(ID province isic3 year) cluster (isic3)
outreg2 using "Tables\TableA9.doc", label dec(3) append ctitle(Excluding agriculture and fishing) addtext(Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)

reghdfe loglabor wt_tariff if trading==1, absorb(ID province isic3 year) cluster (isic3)
outreg2 using "Tables\TableA9.doc", label dec(3) append ctitle(Trading) addtext(Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)


********************************************************************************
*** PART 2: Figures in Online Appendix
********************************************************************************

********************************************************************************
*** Figure B1: The Change in Share of Informal Workers and Tariff Changes
use "Data\master.dta", clear

sum wt_tariff if wt_tariff!=0
sum wt_tariff if wt_tariff!=0 & isic3<=332
sum wt_tariff if wt_tariff!=0 & noag==1
sum wt_tariff if wt_tariff!=0 & trading==1

label variable wt_tariff "The weighted tariff difference"
label variable tariff_manu "Tariff difference * Manufacturing"
label variable manu "Manufacturing"
label variable exp "Experience"
label variable expsquare "Square of Experience"
label variable sex "Female"
label variable schoolyear "Schooling years"
label variable area "Urban"
label variable informal "Informal"
label variable uninsured "Uninsured"
label variable tariff_informal "Tariff difference * Informal workers"
label variable tariff_uninsured "Tariff difference * Uninsured workers"

gen count=1

collapse (mean) wt_tariff (sum) informal uninsured count, by(isic3 year)

gen informals=informal/count
gen uninsureds=uninsured/count

gen tariff_18=wt_tariff if year==2018
gen tariff_19=wt_tariff if year==2019

by isic3, sort: egen wt_tariff18=max(tariff_18)
by isic3, sort: egen wt_tariff19=max(tariff_19)

drop tariff_18 tariff_19

gen informals_17=informals if year==2017
gen uninsureds_17=uninsureds if year==2017

gen informals_14=informals if year==2014
gen uninsureds_14=uninsureds if year==2014


by isic3, sort: egen informal17=max(informals_17)
by isic3, sort: egen uninsured17=max(uninsureds_17)


by isic3, sort: egen informal14=max(informals_14)
by isic3, sort: egen uninsured14=max(uninsureds_14)

drop informals_17 informals_14 uninsureds_17 uninsureds_14

gen informal17_14=informal17-informal14
gen uninsured17_14=uninsured17-uninsured14


collapse (mean) count (first) informal17_14 uninsured17_14 wt_tariff18 wt_tariff19, by(isic3)

label var wt_tariff18 "Tariff Changes in 2018"
label var wt_tariff19 "Tariff Changes in 2019"

drop if informal17_14==. 

regress informal17_14 wt_tariff18 [aweight=count]
predict lfit1

regress informal17_14 wt_tariff19 [aweight=count]
predict lfit2

regress uninsured17_14 wt_tariff18 [aweight=count]
predict lfit3

regress uninsured17_14 wt_tariff19 [aweight=count]
predict lfit4

 

twoway (scatter informal17_14 wt_tariff18 [w=count], msymbol(circle_hollow)) (line lfit1 wt_tariff18), title(The Share of Informal Workers and Tariff Changes in 2018, size(medium)) legend(label(1 "The change in the share of informal workers 2014-2017") rows(2))

graph save "Tables\FigureB1_(1).gph", replace
graph export "Tables\FigureB1_(1).png", replace

twoway (scatter informal17_14 wt_tariff19 [w=count], msymbol(circle_hollow)) (line lfit2 wt_tariff19), title(The Share of Informal Workers and Tariff Changes in 2019, size(medium)) legend(label(1 "The change in the share of informal workers 2014-2017") rows(2))
graph save "Tables\FigureB1_(2).gph", replace
graph export "Tables\FigureB1_(2).png", replace

********************************************************************************
*** Figure B2: The Change in Share of Uninsured Workers and Tariff Changes

twoway (scatter uninsured17_14 wt_tariff18 [w=count], msymbol(circle_hollow)) (line lfit3 wt_tariff18), title(The Share of Uninsured Workers and Tariff Changes in 2018, size(medium)) legend(label(1 "The change in the share of uninsured workers 2014-2017") rows(2))
graph save "Tables\FigureB2_(1).gph", replace
graph export "Tables\FigureB2_(1).png", replace

twoway (scatter uninsured17_14 wt_tariff19 [w=count], msymbol(circle_hollow)) (line lfit4 wt_tariff19), title(The Share of Uninsured Workers and Tariff Changes in 2019, size(medium)) legend(label(1 "The change in the share of uninsured workers 2014-2017") rows(2))
graph save "Tables\FigureB2_(2).gph", replace
graph export "Tables\FigureB2_(2).png", replace

********************************************************************************

*** Figure B3: The Share of Informal Workers in Vietnam by Industry
* Open LFS dataset
use "Data\master.dta", clear

*keep 2017, 2018 and 2019
drop if year<=2016

preserve
generate hhwt_informal = weight_final*informal
collapse (sum) weight_final hhwt_informal, by (isic_name isic1 year)
generate sinformal = (hhwt_informal/weight_final)*100
graph hbar sinformal,  over(year, label(labsize(small))) over(isic_name, sort(isic1) label(labsize(vsmall))) asyvars legend(rows(1) size(vsmall)) ytitle ("The share of informal workers (%)", size(small)) bar(1, color(maroon) fintensity(inten60)) bar(2, color(gray) fintensity(inten40)) bar(3, color(navy) fintensity(inten80)) 
graph export "Tables\FigureB3.png", replace
restore

********************************************************************************
*** Figure B4: The Share of Workers without Insurance in Vietnam by Industry

preserve
generate hhwt_uninsured = weight_final*uninsured
collapse (sum) weight_final hhwt_uninsured, by (isic_name isic1 year)
generate suninsured = (hhwt_uninsured/weight_final)*100
graph hbar suninsured,  over(year, label(labsize(small))) over(isic_name, sort(isic1) label(labsize(vsmall))) asyvars legend(rows(1) size(vsmall)) ytitle ("The share of workers without insurance (%)", size(small)) bar(1, color(maroon) fintensity(inten60)) bar(2, color(gray) fintensity(inten40)) bar(3, color(navy) fintensity(inten80)) 
graph export "Tables\FigureB4.png", replace
restore


********************************************************************************
*** Figure B5: Trade War Impact on Workers' Likelihood of Being in Informal Employment: Permutation Test Results

use "Data\master.dta", clear

drop if year<=2016

sum wt_tariff if wt_tariff!=0
sum wt_tariff if wt_tariff!=0 & isic3<=332
sum wt_tariff if wt_tariff!=0 & noag==1
sum wt_tariff if wt_tariff!=0 & trading==1

label variable wt_tariff "The weighted tariff difference"
label variable tariff_manu "Tariff difference * Manufacturing"
label variable manu "Manufacturing"
label variable exp "Experience"
label variable expsquare "Square of Experience"
label variable sex "Female"
label variable schoolyear "Schooling years"
label variable area "Urban"
label variable informal "Informal"
label variable uninsured "Uninsured"
label variable tariff_informal "Tariff difference * Informal workers"
label variable tariff_uninsured "Tariff difference * Uninsured workers"

gen temp_id=_n

save "Data\master_withid.dta", replace


* Acquire coefficients from true data to compare

*Regression 1
reghdfe informal wt_tariff exp expsquare sex schoolyear area [pweight=weight_final], absorb(isic3 year province) cluster (isic3)


*Regression 2
reghdfe informal wt_tariff exp expsquare sex schoolyear area [pweight=weight_final] if noag==1, absorb(isic3 year province) cluster (isic3)

*Regression 3
reghdfe informal wt_tariff exp expsquare sex schoolyear area [pweight=weight_final] if trading==1, absorb(isic3 year province) cluster (isic3)

*Regression 4
reghdfe uninsured wt_tariff exp expsquare sex schoolyear area [pweight=weight_final], absorb(isic3 year province) cluster (isic3)
 
*Regression 5

reghdfe uninsured wt_tariff exp expsquare sex schoolyear area [pweight=weight_final] if noag==1, absorb(isic3 year province) cluster (isic3)

*Regression 6

reghdfe uninsured wt_tariff exp expsquare sex schoolyear area [pweight=weight_final] if trading==1, absorb(isic3 year province) cluster (isic3)

*Permutation Test

* ESTABLISH A FIXED INITIAL ORDER FOR THE DATA

keep informal uninsured temp_id

save "Data\initial.dta", replace

forval y = 1(1)10000{

*Generate fake outcome variables with shuffled id
use "Data\initial.dta", clear
drop temp_id
set seed `y'
gen double shuffle=runiform()
sort shuffle

gen temp_id=_n
drop shuffle
rename informal informal_fake
rename uninsured uninsured_fake
save "Data\shuffle.dta", replace
clear

*Match master's data with fake outcome variables 
use "Data\master_withid.dta", clear
merge 1:1 temp_id using "Data\shuffle.dta"
keep if _merge==3
drop _merge

*Run regressions using the wrong fake outcome variables and get coefficients

*Regression 1
reghdfe informal_fake wt_tariff exp expsquare sex schoolyear area [pweight=weight_final], absorb(isic3 year province) cluster (isic3)

gen coef_reg1=_b[wt_tariff] in 1


*Regression 2
reghdfe informal_fake wt_tariff exp expsquare sex schoolyear area [pweight=weight_final] if noag==1, absorb(isic3 year province) cluster (isic3)

gen coef_reg2=_b[wt_tariff] in 1


*Regression 3
reghdfe informal_fake wt_tariff exp expsquare sex schoolyear area [pweight=weight_final] if trading==1, absorb(isic3 year province) cluster (isic3)

gen coef_reg3=_b[wt_tariff] in 1


*Regression 4
reghdfe uninsured_fake wt_tariff exp expsquare sex schoolyear area [pweight=weight_final], absorb(isic3 year province) cluster (isic3)
 
gen coef_reg4=_b[wt_tariff] in 1
 

*Regression 5

reghdfe uninsured_fake wt_tariff exp expsquare sex schoolyear area [pweight=weight_final] if noag==1, absorb(isic3 year province) cluster (isic3)

gen coef_reg5=_b[wt_tariff] in 1


*Regression 6

reghdfe uninsured_fake wt_tariff exp expsquare sex schoolyear area [pweight=weight_final] if trading==1, absorb(isic3 year province) cluster (isic3)

gen coef_reg6=_b[wt_tariff] in 1

keep coef_reg1 coef_reg2 coef_reg3 coef_reg4 coef_reg5 coef_reg6
drop if coef_reg1==.
drop if coef_reg2==.
drop if coef_reg3==.
drop if coef_reg4==.

g ranshuffle = `y'

save "Data\fals_`y'.dta", replace
}

clear

use "Data\fals_1.dta", clear
forval x = 2(1)10000{
	append using "Data\fals_`x'.dta"
}

save "Data\falsdata.dta", replace

*Acquire P-value from the counterfactual distribution of coefficients

use "Data\falsdata.dta", clear

sum coef_reg1 if coef_reg1<-0.706
gen pvalue1=r(N)/10000
dis pvalue1


sum coef_reg2 if coef_reg2<-0.672
gen pvalue2=r(N)/10000
dis pvalue2

sum coef_reg3 if coef_reg3<-0.804
gen pvalue3=r(N)/10000
dis pvalue3


sum coef_reg4 if coef_reg4<-0.780
gen pvalue4=r(N)/10000
dis pvalue4


sum coef_reg5 if coef_reg4<-0.724
gen pvalue5=r(N)/10000
dis pvalue5

sum coef_reg6 if coef_reg4<-0.923
gen pvalue6=r(N)/10000
dis pvalue6

*Draw Graphs

*Informal: Figure B5
hist coef_reg1, xscale(range (-0.8 0.5)) color(eltblue)  lcolor(ebblue) xline(-0.706, lcolor(red)) xlabel(0. "0" -0.706 "-0.706", labsize(small)) title("Distribution of Coefficients from Permutation Test: Informal" "All", size(medium)) xtitle("Coefficients of Weighted Tariffs") note("P-value = 0.000" "N(10000)" "Vertical line: The coefficient estimated using true data") 

graph save "Tables\FigureB5_1.gph", replace
graph export "Tables\FigureB5_1.jpeg", as(jpg) quality(100) replace


hist coef_reg2, xscale(range (-0.7 0.5)) color(eltblue) lcolor(ebblue) xline(-0.672, lcolor(red))  xlabel(0. "0" -0.672 "-0.672", labsize(small)) title("Distribution of Coefficients from Permutation Test: Informal" "Excluding Agriculture and Fishing", size(medium)) xtitle("Coefficients of Weighted Tariffs") note("P-value = 0.000" "N(10000)" "Vertical line: The coefficient estimated using true data") 

graph save "Tables\FigureB5_2.gph", replace
graph export "Tables\FigureB5_2.jpeg", as(jpg) quality(100) replace

hist coef_reg3, xscale(range (-0.9 0.5)) color(eltblue) lcolor(ebblue) xline(-0.804, lcolor(red)) xlabel(0. "0" -0.804 "-0.804", labsize(small)) title("Distribution of Coefficients from Permutation Test: Informal" "Trading Sector", size(medium)) xtitle("Coefficients of Weighted Tariffs") note("P-value = 0.000" "N(10000)" "Vertical line: The coefficient estimated using true data") 

graph save "Tables\FigureB5_3.gph", replace
graph export "Tables\FigureB5_3.jpeg", as(jpg) quality(100) replace

********************************************************************************
*** Figure B6: Trade War Impact on Workers' Likelihood of Being Uninsured: Permutation Test Results

hist coef_reg4, xscale(range (-0.8 0.5)) color(eltblue) lcolor(ebblue) xline(-0.780, lcolor(red)) xlabel(0. "0" -0.780 "-0.780", labsize(small)) title("Distribution of Coefficients from Permutation Test: Uninsured" "All", size(medium)) xtitle("Coefficients of Weighted Tariffs") note("P-value = 0.000" "N(10000)" "Vertical line: The coefficient estimated using true data") 

graph save "Tables\FigureB6_1.gph", replace
graph export "Tables\FigureB6_1.jpeg", as(jpg) quality(100) replace


hist coef_reg5, xscale(range (-0.8 0.5)) color(eltblue) lcolor(ebblue) xline(-0.724, lcolor(red)) xlabel(0. "0" -0.724 "-0.724", labsize(small)) title("Distribution of Coefficients from Permutation Test: Uninsured" "Excluding Agriculture and Fishing", size(medium)) xtitle("Coefficients of Weighted Tariffs") note("P-value = 0.000" "N(10000)" "Vertical line: The coefficient estimated using true data") 

graph save "Tables\FigureB6_2.gph", replace
graph export "Tables\FigureB6_2.jpeg", as(jpg) quality(100) replace


hist coef_reg6, xscale(range (-1 0.5)) color(eltblue) lcolor(ebblue) xline(-0.923, lcolor(red)) xlabel(0. "0" -0.923 "-0.923", labsize(small)) title("Distribution of Coefficients from Permutation Test: Uninsured" "Trading Sector", size(medium)) xtitle("Coefficients of Weighted Tariffs") note("P-value = 0.000" "N(10000)" "Vertical line: The coefficient estimated using true data") 

graph save "Tables\FigureB6_3.gph", replace
graph export "Tables\FigureB6_3.jpeg", as(jpg) quality(100) replace


log close
